/*
 * --------------------------------------------------------------------
 * @name MailSender.java
 * @author Juan Camilo Ibarra
 *
 * This work is licensed under a Creative
 * Commons Attribution 3.0 License. This means you can to copy, 
 * distribute, transmit and adapt the work, under the following condition:
 * You must attribute the work in the manner specified by the author or licensor.
 *
 * Juan Camilo Ibarra	-	juanibarral@gmail.com
 * Oct 19, 2011
 * --------------------------------------------------------------------
 */

package util;

import java.util.Date;
import java.util.Map;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 * Class that send mails
 * @author juanibarral
 *
 */
public class MailSender {
	
	private String smtpHostName;
	private int smtpHostPort;
	private String smtpAuthUser;
	private String smtpAuthPwd;

	private String emailMsgBody;
	private String emailMsgSubject;
	
	public MailSender()
	{

	}
	
	public void configure(String configFilepath) throws Exception
	{
		Map<String, String> config = IniFilesReader.readIniFile(configFilepath);
		
		smtpHostName = config.get("smtp.host.name");
		smtpHostPort = Integer.valueOf(config.get("smtp.host.port"));
		smtpAuthUser = config.get("smtp.auth.user");
		smtpAuthPwd = config.get("smtp.auth.pwd");
		
		emailMsgSubject = config.get("email.msg.subject");
		emailMsgBody = config.get("email.msg.body");
		
	}
	
	public void sendMail(String emailMsgTo)throws Exception
	{
		System.out.println("Trying to send mail...");
		final Session session = Session.getInstance(System.getProperties(), null);
		final Message msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress(smtpAuthUser));

		final Address[] recipientAddresses = InternetAddress.parse(emailMsgTo);
		msg.setRecipients(Message.RecipientType.TO, recipientAddresses);

		msg.setSentDate(new Date());
		msg.setSubject(emailMsgSubject);
		msg.setText(emailMsgBody);
		
	

		final Transport transport = session.getTransport("smtps");
		transport.connect(smtpHostName, smtpHostPort, smtpAuthUser, smtpAuthPwd);
		transport.sendMessage(msg, recipientAddresses);
		transport.close();
		System.out.println("Mail sent to " + emailMsgTo);
	}
	
	public void sendMailWithAtt(String emailMsgTo, String filename) throws Exception
	{
		System.out.println("Trying to send mail...");
		final Session session = Session.getInstance(System.getProperties(), null);
		final Message message = new MimeMessage(session);
		message.setFrom(new InternetAddress(smtpAuthUser));

		final Address[] recipientAddresses = InternetAddress.parse(emailMsgTo);
		message.setRecipients(Message.RecipientType.TO, recipientAddresses);

		message.setSentDate(new Date());
		message.setSubject(emailMsgSubject);
		
		// Create the message part 
		BodyPart messageBodyPart = new MimeBodyPart();

		// Fill the message
		messageBodyPart.setText(emailMsgBody);

		Multipart multipart = new MimeMultipart();
		multipart.addBodyPart(messageBodyPart);

		// Part two is attachment
		messageBodyPart = new MimeBodyPart();
		DataSource source = new FileDataSource(filename);
		messageBodyPart.setDataHandler(new DataHandler(source));
		messageBodyPart.setFileName(filename);
		multipart.addBodyPart(messageBodyPart);

		// Put parts in message
		message.setContent(multipart);

		
		final Transport transport = session.getTransport("smtps");
		transport.connect(smtpHostName, smtpHostPort, smtpAuthUser, smtpAuthPwd);
		transport.sendMessage(message, recipientAddresses);
		transport.close();
		System.out.println("Mail sent to " + emailMsgTo + "\nAttachment: " + filename);
	}
	
	public static void main(String[] args)
	{
		MailSender instance = new MailSender();
		try {
			instance.configure("./ext/mail.config");
//			instance.sendMailWithAtt("./Files/femaleFace_001.correo@dominio.com.png");
			instance.sendMail("un_juanibarra@hotmail.com");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
